package com.example.httpclient.Crawler;

import android.os.Handler;
import android.os.Message;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class HtmlTagAttr implements Runnable {
    private static URL URLNet;
    private static HttpURLConnection httpUrlConnection;
    private static String TextString = "";
    private String url, tagName, attr, filter;
    private HtmlTagHandler htmlTagHandler;
    private Handler mHandler;


    public HtmlTagAttr(String url, String tagName, String attr, String filter, Handler mHandler) {
        this.url = url;
        this.tagName = tagName;
        this.attr = attr;
        this.filter = filter;
        this.mHandler = mHandler;
    }

    public HtmlTagAttr setHtmlTagHandler(HtmlTagHandler htmlTagHandler) {
        this.htmlTagHandler = htmlTagHandler;
        return this;
    }
    @Override
    public void run() {
        ArrayList<String> attrStrings = new ArrayList<>();
        boolean isURLok = true;
        BufferedReader br = null;
        InputStreamReader isr = null;
        mHandler.sendEmptyMessage(0x221);
        try {
            URLNet = new URL(url);
            httpUrlConnection = (HttpURLConnection) URLNet.openConnection();
            if (httpUrlConnection.getResponseCode() == 200) {
                Message message = new Message();
                message.what = 0x00;
                message.obj = "网址打开成功";
                mHandler.sendMessage(message);
                isr = new InputStreamReader(httpUrlConnection.getInputStream(), "utf-8");
                br = new BufferedReader(isr);
                String readTxt = "";
                while ((readTxt = br.readLine()) != null) {
                    attrStrings.add(readTxt);
                }
            }
        } catch (IOException e) {
            Message message = new Message();
            message.what = 0x00;
            isURLok = false;
            message.obj = "网址打开失败";
            mHandler.sendMessage(message);
            mHandler.sendEmptyMessage(0x222);
        }

        if (isURLok) {
            if (tagName != null && tagName.length() != 0) {
                attrStrings = new ArrayList<>();
                boolean isOK = true;
                Document document = null;
                try {
                    document = Jsoup.parse(URLNet, 5 * 1000);

                } catch (IOException e) {
                    Message message = new Message();
                    message.what = 0x00;
                    isOK = false;
                    message.obj = "网页解析超时";
                    mHandler.sendMessage(message);
                    mHandler.sendEmptyMessage(0x222);
                    e.printStackTrace();
                }
                if (isOK) {
                    Elements tags = document.getElementsByTag(tagName);
                    attrStrings.add(tags.toString());
                    if (attr.length() != 0 && attr != null) {
                        attrStrings = new ArrayList<>();
                        for (Element tag : tags) {
                            attrStrings.add(tag.attr(attr));
                        }
                        if (filter != null && filter.length() != 0) {
                            ArrayList<String> tempList = attrStrings;
                            attrStrings = new ArrayList<>();
                            for (String ss : tempList) {
                                if (ss.contains(filter)) {
                                    attrStrings.add(ss);
                                }
                            }

                        }
                    }
                }
            }
            htmlTagHandler.TagHandler(attrStrings);
            mHandler.sendEmptyMessage(0x222);
        }
    }
}
